var add,getUnBoundDrvPage;
require(['grid', 'topBar', 'layout','select2'], function () {
	 var drvIds=[],$top = $('#top'),$grid = $('#grid');
    //顶部栏初始化
    function topBarInit() {
        $top.topBar({
            left: [
                   {
                       text: '姓名',
                       name: 'qdrvName',
                       width: 80,
                       type: 'text'
                   },
                   {
                       text: '工号',
                       name: 'qworkEmpeEmpno',
                       width: 80,
                       type: 'text'
                   },
                   {
                       text: '考勤卡卡号',
                       name: 'qtimecardCardno',
                       width: 80,
                       type: 'text'
                   },
                   {
                    text: '查询',
                    type: 'btn',
                    click: function () {
                    	carIds = commonFun.getLeftTreeCheckedNodes().CAR;
    					if(typeof(carIds) == 'undefined'){ // 左侧树没有选中车辆
    						$.messager.alert("提示","请选中一辆车，进行添加驾驶员。","info");
    					}else if(carIds.length > 1){ // 左侧树选中车辆大于一辆
    						$.messager.alert("提示","您只能选中一辆车，进行添加驾驶员。","info");
    					}else{
    						getUnBoundDrvPage(carIds[0]);
    					}
                    }
                }
            ]
        });
    }
    //grid初始化
    function gridInit() {
	    //未绑定驾驶员列表
	    $grid.grid({
	        url: "/carDrvRel/getUnBoundDrvPage.htm",
	        colModel: [
	            {name: "drvId",label: 'drvId',hidden: true},
	            {name: "drvName",label: '姓名', width: 80},
	            {name: "organName",label: '所属机构', width: 80},
	            {name: "workEmpeEmpno",label: '员工工号', width: 80,align:'right'},
	            {name: "timecardCardno",label: '考勤卡卡号', width: 80,align:'right'},
	            {name: "birthDate",label: '出生日期', width: 100,align:'right'},
	            {name: "gender",label: '性别', width: 50, dict: "SEX00000"},
	            {name: "driLicType",label: '驾驶证类型', width: 80, dict: "DLT00000"},
	            {name: "driLicNo",label: '驾驶证号', width: 80, dict: "EML00000"},
	            {name: "st",label: '状态', width: 80, dict: "EMS00000"}
	        ],
	        multiselect: true
	    });
    }

     add = function(fn) {
	    var ids = $grid.grid('getGridParam','selarrrow');
		if(ids.length == 0){ // 左侧树没有选中驾驶员
			$.messager.alert("提示","您还没有选中驾驶员。","info");
		}else{
			drvIds = [];
			$.each(ids,function(i){
				drvIds.push($grid.grid('getRowData',ids[i]).drvId);
		   	});
			$.messager.confirm("是否添加","是否为选中车辆添加驾驶员？",function(r){
				if(r){
					var carId = commonFun.getLeftTreeCheckedNodes("CAR").toString();
					$.ajaxExt({
						url : "/carDrvRel/bindDrvsToCar.htm",
						data : {'drvIds':drvIds,'carId':carId}
					}).done(function(){
						if(fn){
							fn();
						}
					});
				}
			});
		}
   };
    
   //查询未绑定的驾驶员
   getUnBoundDrvPage = function(carId){
	   $.ajaxExt({
			url : "/carDrvRel/getCarInfoByCarId.htm",
			data : { 'carId': carId }
		}).done(function(data){
			var organId = data.organId; // 要添加驾驶员的车辆所在的机构
			$grid.grid('setGridParam',{
				page:1,
				datatype:"json",
				postData:{
					"organId":organId,
					"qdrvName":$top.getValues().qdrvName,
					"qworkEmpeEmpno":$top.getValues().qworkEmpeEmpno,
					"qtimecardCardno":$top.getValues().qtimecardCardno
				}
			}).trigger("reloadGrid");
		});
	};
	
    //layout 布局
    $('body').layout();

    topBarInit();
    gridInit();
});